﻿using System;
using System.Text;
using Core.Library;
using DataObject;
using DataObject.Article;
using DataObject.Category;

namespace KMT.Controls.Common
{
    public partial class Navigation : System.Web.UI.UserControl
    {
        private StringBuilder _sbProduct = new StringBuilder();
        private StringBuilder _sbArticle = new StringBuilder();
        protected string Title = string.Empty;
        protected string Sapo  = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ListProductCategory();

            ListArticleCategory();

            ArticleHighlight();
        }

        /// <summary>
        /// Danh mục sản phẩm
        /// </summary>
        private void ListProductCategory()
        {
            var lstLevel0 = CategoryController.Category_GetByParentId((int) CategoryType.Product, Constants.PARENT_ROOT, Constants.ACTIVE);

            if (lstLevel0.Count > 0)
            {
                foreach (CategoryObject level0 in lstLevel0)
                {
                    _sbProduct.AppendFormat("<li class=\"parent\"><a href=\"{0}\" class=\"level-top\"><span>{1}</span></a>", "#", level0.Name);

                    var lstLevel1 = CategoryController.Category_GetByParentId((int)CategoryType.Product, level0.CategoryId, Constants.ACTIVE);
                    if (lstLevel1.Count > 0)
                    {                        
                        _sbProduct.Append("<ul class=\"level0 chield\">");

                        foreach (CategoryObject level1 in lstLevel1)
                        {
                            _sbProduct.AppendFormat("<li class=\"parent\"><a href=\"{0}\"><span>{1}</span></a>",
                                                        UrlHelpers.ListProductByCategory(level1.CategoryId, level1.Url), level1.Name);

                            var lstLevel2 = CategoryController.Category_GetByParentId((int)CategoryType.Product, level1.CategoryId, Constants.ACTIVE);
                            if (lstLevel2.Count > 0)
                            {
                                _sbProduct.Append("<ul>");

                                foreach (CategoryObject level2 in lstLevel2)
                                    _sbProduct.AppendFormat("<li><a href=\"{0}\"><span>{1}</span></a>",
                                                        UrlHelpers.ListProductByCategory(level2.CategoryId, level2.Url), level2.Name);                                                                        

                                _sbProduct.Append("</ul>");
                            }
                            // level 2
                            _sbProduct.Append("</li>");

                        }
                        _sbProduct.Append("</ul>");
                    }
                    // level 1

                    _sbProduct.Append("</li>");
                    // level 0
                }                
            }

            ltProduct.Text = _sbProduct.ToString();
        }

        /// <summary>
        /// Danh mục tin tức
        /// </summary>
        
        private void ListArticleCategory()
        {
            var lstLevel0 = CategoryController.Category_GetListByType_ListId((int) CategoryType.Article, Constants.Category_FieldList, Constants.GetStringFromWebConfig("CategoryHeaderArticle"));

            if (lstLevel0 != null && lstLevel0.Rows.Count > 0)
            {
                for (int i = 0; i < lstLevel0.Rows.Count; i++)
                {
                    _sbArticle.AppendFormat("<li class=\"level1 parent\"><a href=\"{0}\" class=\"level-top\"><span>{1}</span></a>", "#", lstLevel0.Rows[i]["Name"]);                    
                                        var lstLevel1 = CategoryController.Category_GetByParentId((int)CategoryType.Article, ConvertUtility.ToInt32(lstLevel0.Rows[i]["CategoryId"]), Constants.ACTIVE);
                    if (lstLevel1.Count > 0)
                    {
                        _sbArticle.Append("<ul>");

                        foreach (CategoryObject level1 in lstLevel1)
                            _sbArticle.AppendFormat("<li><a href=\"{0}\"><span>{1}</span></a></li>", "#", level1.Name);

                        _sbArticle.Append("</ul>");
                    }
                    // level 1

                    _sbArticle.Append("</li>");
                    // level 0
                }                
            }

            ltArticle.Text = _sbArticle.ToString();
        }


        /// <summary>
        /// Tiêu điểm bài viết
        /// </summary>
        private void ArticleHighlight()
        {
            var lstArticle = ArticleController.Article_GetTopHighlight(SettingValues.PageSize_1);
            if (lstArticle.Count > 0)
            {
                var categoryName = CategoryController.GetCategoryName(lstArticle[0].CategoryId);
                var articleHref  = UrlHelpers.ArticleDetail(categoryName, ConvertUtility.ToInt32(lstArticle[0].ArticleId), lstArticle[0].Title);

                var imgHref = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size=186", 
                                                Constants.ThumbImage, lstArticle[0].ArticleId, (int)CategoryType.Article);
                ltImage.Text = string.Format("<a href=\"{0}\"><img style=\"float: left; margin: 0 20px 10px 0;\" src=\"{1}\" alt=\"\" /></a>", 
                                                articleHref, imgHref);

                Title = lstArticle[0].Title;
                Sapo  = lstArticle[0].Sapo;
            }
        }
        
    }
}